package core;

import java.util.ArrayList;
import java.util.HashMap;

public class Orm {
	DbManager _db = DbManager.getInstance();
	private String		table = new String();
	
	private ArrayList<HashMap<String, String>>	whereClause = new ArrayList<HashMap<String,String>>();
	private ArrayList<HashMap<String, String>>	groupClause = new ArrayList<HashMap<String,String>>();
	
	public 			Orm(String _table){
		table = _table;
		_db.addTable(table);
	}
	
	public Object	execute(){
		return (GenerObject.getItBaby(_db.loadHeavyObject(whereClause, groupClause)));
	}
	
	public Orm	find(String col, String value){
		HashMap<String, String>	hash = new HashMap<String, String>();
		hash.put("type", "FIND");
		hash.put("value", "`"+table+"`."+col+"='"+value+"' ");
		
		whereClause.add(hash);
		return (this);
	}
	
	public Orm	findPK(String pk){
		HashMap<String, String>	hash = new HashMap<String, String>();
		hash.put("type", "PK");
		hash.put("value", "`"+table+"`.[PKID]='"+pk+"' ");
		
		whereClause.add(hash);
		return (this);
	}
	
	public Orm	limitRows(int start, int end){		
		HashMap<String, String>	hash = new HashMap<String, String>();
		hash.put("type", "LIMIT");
		hash.put("value", "LIMIT "+start+", "+end);
		
		groupClause.add(hash);
		return (this);
	}
	
	public Orm	and(){
		HashMap<String, String>	hash = new HashMap<String, String>();
		hash.put("type", "LINKER");
		hash.put("value", "AND ");
		
		whereClause.add(hash);
		return (this);
	}
	
	public Orm	or(){
		HashMap<String, String>	hash = new HashMap<String, String>();
		hash.put("type", "LINKER");
		hash.put("value", "OR ");
		
		whereClause.add(hash);
		return (this);
	}
}
